package com.forlove.app.qselect.index.presenter;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.forlove.app.common.base.mvp.presenter.IBasePresenter;
import com.forlove.app.common.network.ICallBack;
import com.forlove.app.common.utils.LogUtil;
import com.forlove.app.common.utils.ParseHelper;
import com.forlove.app.common.utils.StringUtils;
import com.forlove.app.manager.DataManager;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.manager.ServerManager;
import com.forlove.app.qselect.CommonEntity;
import com.forlove.app.qselect.index.entity.PageEntity;
import com.forlove.app.qselect.index.entity.RankEntity;
import com.forlove.app.qselect.index.service.IMineInfoService;
import com.forlove.app.qselect.index.service.impl.MinInfoServiceImpl;
import com.forlove.app.qselect.index.view.MineInfoView;
import com.forlove.app.qselect.login.entity.SchoolDto;
import com.forlove.app.qselect.login.entity.UserEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yjl on 2017/10/26.
 */

public class MineInfoPresenter extends IBasePresenter {
    private MineInfoView mineInfoView;
    private IMineInfoService mineInfoService;
    private List<RankEntity> rankList = new ArrayList<>();

    public MineInfoPresenter(MineInfoView mineInfoView) {
        this.mineInfoView = mineInfoView;
        if (mineInfoService == null) {
            mineInfoService = new MinInfoServiceImpl();
            setService(mineInfoService);
        }
    }


    public void loadDetailData(Map param) {
        mineInfoService.loadData(ServerManager.ACTION_PERSON_DETAIL_INFO, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "个人详细信息:" + result);
                UserEntity userEntity = ParseHelper.getModel(result, "data.user", UserEntity.class);
                if (mineInfoView != null) {
                    mineInfoView.updateMineInfo(userEntity);
                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    if (mineInfoView != null) {
                        mineInfoView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                    }
                } else {
                    if (mineInfoView != null) {
                        mineInfoView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                    }
                }
            }
        });
    }


    public synchronized void loadRankingList(final PageEntity pageEntity) {
        if (pageEntity == null) {
            return;
        }
        Map param = new HashMap();
        param.put("mobile", pageEntity.mobile + "");
        param.put("page", pageEntity.mPage);
        mineInfoService.loadData(ServerManager.ACTION_PERSON_RANKLIST, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                List<RankEntity> rankEntities = ParseHelper.getModelList(result, "data.list", RankEntity.class);
                if (pageEntity.mPage > 1
                        && (rankEntities == null || rankEntities.size() == 0)) {
                    pageEntity.mPage--;
                } else if (pageEntity.mPage == 1 && rankEntities != null && rankEntities.size() > 0) {
                    rankList.clear();
                    rankList.addAll(rankEntities);
                } else if (pageEntity.mPage > 1 && rankEntities != null && rankEntities.size() > 0) {
                    rankList.addAll(rankEntities);
                }

                if (mineInfoView != null) {
                    mineInfoView.updataRanklistView(pageEntity, rankList);
                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    if (mineInfoView != null) {
                        mineInfoView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                    }
                } else {
                    if (mineInfoView != null) {
                        mineInfoView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                    }
                }
            }
        });
    }

    public synchronized void loadMoreRankList(PageEntity pageEntity) {
        pageEntity.mPage++;
        loadRankingList(pageEntity);
    }


    public synchronized void loadDeleteFirend(Map param) {
        mineInfoService.loadData(ServerManager.ACTION_DELETE_FRIENDS, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                mineInfoView.updateDelteView(commonEntity);
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    if (mineInfoView != null) {
                        mineInfoView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                    }
                } else {
                    if (mineInfoView != null) {
                        mineInfoView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                    }
                }
            }
        });
    }

    public synchronized void loadReportFriend(Map param) {
        mineInfoService.loadData(ServerManager.ACTION_REPORT_FRIENDS, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                mineInfoView.updateReportView(commonEntity);
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    if (mineInfoView != null) {
                        mineInfoView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                    }
                } else {
                    if (mineInfoView != null) {
                        mineInfoView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                    }
                }
            }
        });
    }

    public void loadSchoolInfo() {
        mineInfoService.loadData(ServerManager.ACTION_GET_SCHOOL, new HashMap(), new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "学校信息:" + result);
                String school = ParseHelper.getString(result, "data");
                if (StringUtils.isNotEmpty(school)) {
                    SchoolDto schoolDto = JSON.parseObject(school, SchoolDto.class);
                    if (schoolDto != null) {
                        DataManager.saveSchoolInfo(school);
                    }
                    mineInfoView.updateSchoolInfo(schoolDto.getData());
                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    mineInfoView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                } else {
                    mineInfoView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                }
            }
        });
    }
}
